package com.ecloud.support.generator.custom;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.ecloud.common.base.model.BaseDto;
import com.ecloud.common.base.model.BaseVo;
import com.ecloud.support.generator.model.dto.TableConfigDto;

public class CustomerConfigUtils {

    public static Map<String, Object> initDto(TableInfo tableInfo, TableConfigDto dto) {
        Set<String> prefix = new HashSet<>(Arrays.asList(dto.getPrefix()));
        Set<String> pcks = new HashSet<>();
        pcks.add(BaseDto.class.getCanonicalName());
        pcks.addAll(tableInfo.getImportPackages());
        Set<String> removes = new HashSet<>();
        for (Class<?> clz : CustomerConstVal.DTO_REMOVE_CLASS) {
            removes.add(clz.getCanonicalName());
        }
        pcks.removeAll(removes);
        Map<String, Object> map = new HashMap<>();
        map.put(CustomerConstVal.CUSTOMER_CONFIG_NAME,
                NamingStrategy.capitalFirst(NamingStrategy.removePrefixAndCamel(tableInfo.getName(), prefix)) + CustomerConstVal.DTO_SUFFIX);
        map.put(CustomerConstVal.CUSTOMER_CONFIG_PCK, dto.getPackageName() + CustomerConstVal.PKG_SPLIT_REPLACE + dto.getDtoPackage());
        map.put(CustomerConstVal.CUSTOMER_CONFIG_PCKS, pcks.toArray(new String[pcks.size()]));
        map.put(CustomerConstVal.CUSTOMER_CONFIG_SUPER, BaseDto.class.getSimpleName());
        return map;
    }

    public static Map<String, Object> initVo(TableInfo tableInfo, TableConfigDto dto) {
        Set<String> prefix = new HashSet<>(Arrays.asList(dto.getPrefix()));
        Set<String> pcks = new HashSet<>();
        pcks.add(BaseVo.class.getCanonicalName());
        pcks.addAll(tableInfo.getImportPackages());
        Set<String> removes = new HashSet<>();
        for (Class<?> clz : CustomerConstVal.VO_REMOVE_CLASS) {
            removes.add(clz.getCanonicalName());
        }
        pcks.removeAll(removes);
        Map<String, Object> map = new HashMap<>();
        map.put(CustomerConstVal.CUSTOMER_CONFIG_NAME,
                NamingStrategy.capitalFirst(NamingStrategy.removePrefixAndCamel(tableInfo.getName(), prefix)) + CustomerConstVal.VO_SUFFIX);
        map.put(CustomerConstVal.CUSTOMER_CONFIG_PCK, dto.getPackageName() + CustomerConstVal.PKG_SPLIT_REPLACE + dto.getVoPackage());
        map.put(CustomerConstVal.CUSTOMER_CONFIG_PCKS, pcks.toArray(new String[pcks.size()]));
        map.put(CustomerConstVal.CUSTOMER_CONFIG_SUPER, BaseVo.class.getSimpleName());
        return map;
    }

    public static Map<String, Object> initConvert(TableInfo tableInfo, TableConfigDto dto) {
        Set<String> prefix = new HashSet<>(Arrays.asList(dto.getPrefix()));
        String tempName = NamingStrategy.capitalFirst(NamingStrategy.removePrefixAndCamel(tableInfo.getName(), prefix));
        Set<String> pcks = new HashSet<>();
        pcks.add(List.class.getCanonicalName());
        pcks.add(Mapper.class.getCanonicalName());
        pcks.add(Mappers.class.getCanonicalName());
        pcks.add(dto.getPackageName() + CustomerConstVal.PKG_SPLIT_REPLACE + dto.getEntityPackage() + CustomerConstVal.PKG_SPLIT_REPLACE
                + String.format(dto.getEntityName(), tempName));
        pcks.add(dto.getPackageName() + CustomerConstVal.PKG_SPLIT_REPLACE + dto.getDtoPackage() + CustomerConstVal.PKG_SPLIT_REPLACE + tempName
                + CustomerConstVal.DTO_SUFFIX);
        pcks.add(dto.getPackageName() + CustomerConstVal.PKG_SPLIT_REPLACE + dto.getVoPackage() + CustomerConstVal.PKG_SPLIT_REPLACE + tempName
                + CustomerConstVal.VO_SUFFIX);
        Map<String, Object> map = new HashMap<>();
        map.put(CustomerConstVal.CUSTOMER_CONFIG_NAME,
                NamingStrategy.capitalFirst(NamingStrategy.removePrefixAndCamel(tableInfo.getName(), prefix)) + CustomerConstVal.CONVERT_SUFFIX);
        map.put(CustomerConstVal.CUSTOMER_CONFIG_PCK, dto.getPackageName() + CustomerConstVal.PKG_SPLIT_REPLACE + dto.getConvertPackage());
        map.put(CustomerConstVal.CUSTOMER_CONFIG_PCKS, pcks.toArray(new String[pcks.size()]));
        return map;
    }
}
